import cors from 'cors'
import express from 'express'
import { createProxyMiddleware } from 'http-proxy-middleware'

const app = express()

app.use(cors())
app.use(express.json())

// 路由转发配置
const routes = {
  '/api/auth': 'http://auth:3001',
  '/api/chat': 'http://chat:3002',
  '/api/live': 'http://live:3003',
  '/api/game': 'http://game:3004',
  '/api/user': 'http://user:3005',
  '/api/admin': 'http://admin:3006',
}

// 设置代理
Object.entries(routes).forEach(([path, target]) => {
  app.use(
    path,
    createProxyMiddleware({
      target,
      changeOrigin: true,
      pathRewrite: { [`^${path}`]: '' },
    })
  )
})

const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
  console.log(`Gateway running on port ${PORT}`)
})
